﻿using System;

using Gonte.Commands;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MyUtilitiesTest
{
    /// <summary>
    ///This is a test class for AsynchronousCommandTest and is intended
    ///to contain all AsynchronousCommandTest Unit Tests
    ///</summary>
    [TestClass()]
    public class AsynchronousCommandTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        struct MessageHolder
        {
            public String Message;
        }

        [TestMethod()]
        public void AsynchronousCommandOnSuccessTest()
        {
            MessageHolder et = new MessageHolder();
            AsynchronousCommand<MessageHolder, MessageHolder> command = new AsynchronousCommand<MessageHolder, MessageHolder>
            (
                et,
                delegate(MessageHolder p)
                {
                    et.Message = "Invoker, ";
                    return et;
                },
                delegate(MessageHolder r)
                {
                    et.Message += "Callback";
                },
                null,
                null
            );

            command.Execute();

            while (!command.AsyncResult.AsyncWaitHandle.WaitOne(100, true))
            {
                Console.WriteLine("Working");
            }

            Assert.AreEqual("Invoker, Callback", et.Message);
        }

        [TestMethod()]
        public void AsynchronousCommandOnExceptionTest()
        {
            MessageHolder et = new MessageHolder();
            AsynchronousCommand<MessageHolder, MessageHolder> command = new AsynchronousCommand<MessageHolder, MessageHolder>
            (
                et,
                delegate(MessageHolder p)
                {
                    et.Message = "Throwing an exception, ";
                    throw new Exception();
                },
                null,
                delegate(Exception ex)
                {
                    et.Message += "Catching an exception, ";
                },
                delegate(MessageHolder r)
                {
                    et.Message += "Cleanup";
                }
            );

            command.Execute();

            while (!command.AsyncResult.AsyncWaitHandle.WaitOne(100, true))
            {
                Console.WriteLine("Working");
            }

            Assert.AreEqual("Throwing an exception, Catching an exception, Cleanup", et.Message);
        }

        [TestMethod()]
        public void AsynchronousCommandOnCleanUpTest()
        {
            MessageHolder et = new MessageHolder();
            AsynchronousCommand<MessageHolder, MessageHolder> command = new AsynchronousCommand<MessageHolder, MessageHolder>
            (
                et,
                delegate(MessageHolder m)
                {
                    et.Message = "Invoker, ";
                    return m;
                },
                delegate(MessageHolder m)
                {
                    et.Message += "Callback, ";
                },
                null,
                delegate(MessageHolder m)
                {
                    et.Message += "Cleanup";
                }
            );

            command.Execute();

            while (!command.AsyncResult.AsyncWaitHandle.WaitOne(100, true))
            {
                Console.WriteLine("Working");
            }

            Assert.AreEqual("Invoker, Callback, Cleanup", et.Message);
        }
    }
}
